package edu.ccut.saturn.basecomponent.database.util;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import edu.ccut.saturn.basecomponent.database.DatabaseAttributeUtils;
import edu.ccut.saturn.basecomponent.database.IDatabaseAttribute;
import edu.ccut.saturn.basecomponent.database.SaturnSQLBuilderFactory;
import edu.ccut.saturn.basecomponent.database.impl.db2.DB2Attribute;
import edu.ccut.saturn.component.IDataInfo;
import edu.ccut.saturn.component.IDataItem;
import edu.ccut.saturn.component.SaturnComponentException;
import edu.ccut.saturn.component.SaturnData;
import edu.ccut.saturn.component.SaturnDataManager;

public class SaturnDataUtil {
	/**
	 * 获取表名,如果表名为null，则抛出SaturnComponentException异常
	 * 
	 * @param dataInfo
	 * @return
	 */
	public static String getTableName(IDataInfo dataInfo) throws Exception {

		for (String key : dataInfo.keySet()) {

			IDataItem iDataItem = dataInfo.getDataItem(key);

			String tableName = iDataItem.getTableName();

			if (tableName != null && !"".equals(tableName.trim())) {
				return tableName;
			}
		}
		throw new SaturnComponentException(
				"Can't not find table name in SaturnData name is"
						+ dataInfo.getDataFullKey());
	}

	/**
	 * 返回主键
	 * 
	 * @param saturnData
	 * @return
	 */
	public static IDataItem getPKDataItem(SaturnData saturnData) {
		IDataInfo dataInfo = saturnData.getSaturnDataInfo();
		return getPKDataItem(dataInfo);
	}

	/**
	 * 返回主键
	 * 
	 * @param saturnData
	 * @return
	 */
	public static IDataItem getPKDataItem(IDataInfo dataInfo) {

		for (String key : dataInfo.keySet()) {

			IDataItem dataItem = dataInfo.getDataItem(key);

			if ("true".equals(dataItem.getIsPrimaryKey())) {
				return dataItem;
			}
		}
		return null;
	}

	/**
	 * 返回主键
	 * 
	 * @param saturnData
	 * @return
	 */
	public static IDataItem getPKDataItem(String fullKey) {
		IDataInfo dataInfo = SaturnDataManager.getInstance().getSaturnDataInfo(
				fullKey);
		return getPKDataItem(dataInfo);
	}

	/**
	 * 返回一个数据项中指定数据库类型
	 * 
	 * @param dataInfo
	 *            数据项
	 * @param type
	 *            属性类型
	 * @return
	 */
	public static List<String> getColumnList(IDataInfo dataInfo, String type) {

		if (type != null) {
			List<String> list = new ArrayList<String>();

			for (String key : dataInfo.keySet()) {

				IDataItem dataItem = dataInfo.getDataItem(key);

				if (type.trim().equals(dataItem.getType())) {

					list.add(dataItem.getColumn());
				}
			}
			return list;
		}
		return null;
	}

	/**
	 * 根据数据项/fullkey得到格式化后的标准数据库类型(mysql,oracle,sqlserver,db2)
	 * 
	 * @author zjh
	 * 
	 * @param object
	 *            可以是String,SaturnData
	 * @return 标准格式的数据库类型
	 */
	public static String getStandardDBType(Object object) throws Exception {
		String dbType = getDBType(object).toLowerCase();

		if (SaturnSQLBuilderFactory.dbTypeList.contains(dbType)) {
			return dbType;
		} else {
			return SaturnSQLBuilderFactory.ORACLE;
		}
	}

	/**
	 * 判断是否含有大数据
	 * 
	 * @author zjh
	 * 
	 * @param Object
	 *            可以是fullKey 或者是SaturnData
	 * 
	 * @return boolean
	 * 
	 */
	public static boolean hasLOB(Object object) {

		SaturnData saturnData = getSaturnData(object);

		IDataInfo dataInfo = saturnData.getSaturnDataInfo();

		String dbType = dataInfo.getDBType().toLowerCase();

		IDatabaseAttribute dbAttribute = DatabaseAttributeUtils
				.getDatabaseAttribute(dbType);

		for (String key : dataInfo.keySet()) {

			IDataItem dataItem = dataInfo.getDataItem(key);

			if (dbAttribute.isLOB(dataItem.getType())) {
				return true;
			}
		}
		return false;
	}

	/**
	 * 给出fullKey或者SaturnData 统一得到SaturnData 对象
	 * 
	 * @author zjh
	 * 
	 * @param Object
	 *            fullKey 或者 SaturnData
	 * 
	 */
	public static SaturnData getSaturnData(Object object) {
		SaturnData saturnData = null;

		if (object instanceof SaturnData) {

			saturnData = (SaturnData) object;

		} else {
			saturnData = SaturnDataManager.getInstance().createData(
					(String) object);
		}
		return saturnData;
	}

	/**
	 * 给出fullKey或者SaturnData 统一得到dataInfo 对象
	 * 
	 * @author zjh
	 * 
	 * @param Object
	 *            fullKey 或者 SaturnData
	 * 
	 */
	public static IDataInfo getDataInfo(Object object) {
		return getSaturnData(object).getSaturnDataInfo();
	}

	/**
	 * 给出fullKey或者SaturnData 统一得到DBType 对象
	 * 
	 * @author zjh
	 * 
	 * @param Object
	 *            fullKey 或者 SaturnData
	 * 
	 */
	public static String getDBType(Object object) {
		return getDataInfo(object).getDBType();
	}

	/**
	 * 通过fullKey和PrimaryKeyValue得到适合不同数据库的条件语句
	 * 
	 * @author zjh
	 * 
	 * @param fullKey
	 * 
	 * @param primaryKeyValue
	 * 
	 * @return string
	 */
	public static String getConditionWithPrimaryKeyByDbType(String fullKey,
			String primaryKeyValue) throws Exception {
		if (fullKey == null) {
			throw new SaturnComponentException("The fullKey is null");
		}
		IDataItem dataItem = getPKDataItem(fullKey);
		if (dataItem == null) {
			throw new SaturnComponentException(
					"Can not find primaryKey in SaturnData name is" + fullKey);
		}
		if (primaryKeyValue == null) {
			return "";
		}
		String result = null;
		String dbType = getStandardDBType(fullKey);
		if (SaturnSQLBuilderFactory.DB2.equalsIgnoreCase(dbType)) {
			String type = dataItem.getType();
			result = " WHERE " + dataItem.getColumn() + " = "
					+ DB2Attribute.quote(type) + primaryKeyValue
					+ DB2Attribute.quote(type);
		} else {
			result = " WHERE " + dataItem.getColumn() + " ='" + primaryKeyValue
					+ "'";
		}
		return result;
	}

	public static String getStandardDBTypeByConn(Connection conn) throws SQLException {
		return conn.getMetaData().getDatabaseProductName().toLowerCase();
	}

}
